/* Nokia 6100 Color LCD routines
   Author: Martin Maly, www.uelectronics.info, author@uelectronics.info

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


  Target: Luminary Micro LM3S811
  Family: ARM7-TDMI Cortex M3
  Compiler: Keil ARM C
*/

//First demo
//demonstrates basic routines

#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "../diag.h"
#include "../osram96x16.h"
#include "lcd.h"
#include "font.h"
#include "lcd12.h"

#define USER_LED GPIO_PIN_5

void dly2(int a){
 long int i;
 long int j;
 j=10000*a;
 for (i=0;i<j;i++){ ; }
}


//buffer for full color animation
word buffer[32][32];

void clear(word *buffer){
 int i;
 for(i=0;i<50*50;i++) buffer[i]=0;
}

int main(void){
    // Luminary Micro LM3S811 ARM7 chip specific startup
    // Set the clocking to run directly from the crystal.

	int dly=1;
	int i,j;

    SysCtlClockSet(SYSCTL_SYSDIV_2 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
				   SYSCTL_INT_OSC_DIS |	
                   SYSCTL_XTAL_6MHZ);

	//Set peripherials for some demo stuff (LED blinking)
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	GPIODirModeSet(GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_OUT);

	//LCD startup
	LCD_init(); 


//Drawing the Czech national flag in the center
//of the display
//White rectangle
LCD12_rectangle(0,33,131,65,0x0fff);
//Red rectangle
LCD12_rectangle(0,66,131,98,0x0f00);
//Blue triangle
for (i=0;i<33;i++) LCD12_hline(0,i<<1,33+i,0x000f);
for (i=0;i<33;i++) LCD12_hline(0,(33-i)<<1,66+i,0x000f);

//Draw full color shaded rectangles to the bottom
//of the display

for (i=0;i<16;i++) {
	LCD12_hline(0,19,i+114,i);
	LCD12_hline(20,39,i+114,i*16);
	LCD12_hline(40,59,i+114,i*256);
	LCD12_hline(60,79,i+114,i*17);
	LCD12_hline(80,99,i+114,i*0x0101);
	LCD12_hline(100,119,i+114,i*0x0110);
}

//write some text on the display

LCD12_char(10,10,0x0f00,0x0008,'H');
LCD12_char(18,10,0x0f00,0x0008,'e');
LCD12_char(26,10,0x0f00,0x0008,'l');
LCD12_char(34,10,0x0f00,0x0008,'l');
LCD12_char(42,10,0x0f00,0x0008,'o');
LCD12_char(50,10,0x0f00,0x0008,'!');
LCD12_string(0,0,0x0fff,0x000f,"How are you?");

//Switch to 8bit-per-pixel mode
LCD_8bpp();
//prepare default 3-3-2 palette
//it means every byte has the "rrrg ggbb" format (3 bits
//for red, 3 bits for green and 2 bits for blue)
LCD_palette();

//Drawing the same color rectangles as above to show the lesser
//color depth
for (i=0;i<16;i++) {
	LCD_hline(0,19,i+99,color8(i));
	LCD_hline(20,39,i+99,color8(i*16));
	LCD_hline(40,59,i+99,color8(i*256));
	LCD_hline(60,79,i+99,color8(i*0x0011));
	LCD_hline(80,99,i+99,color8(i*0x0101));
	LCD_hline(100,119,i+99,color8(i*0x0110));
}

//Draw the cross - just demo of general LINE function
LCD_line(0,0,130,130,0xfc);
LCD_line(0,130,130,0,0x1f);
//demo of CIRCLE functions
LCD_circle(66,66,50,0xe3);
LCD_circle_fill(66,66,20,0xf1);

//back to full color mode
LCD_12bpp();

//endless animation loop
dly=0;
while(1){
 GPIOPinWrite(GPIO_PORTC_BASE, USER_LED, USER_LED);
 LCD12_blit(98,0,32,32,(word *)buffer);
 dly2(dly);
 GPIOPinWrite(GPIO_PORTC_BASE, USER_LED, 0x00);
 dly2(dly);

 clear((word *)buffer);

 buffer[dly][10]=0x0ff0;
 buffer[dly][11]=0x0ff5;
 buffer[dly][12]=0x0ffa;
 buffer[dly][13]=0x0fff;
 dly++;
 if (dly==32) dly=0;
}
}



